package net.zjitc.controller;

import net.zjitc.pojo.Admin;
import net.zjitc.service.AdminService;
import net.zjitc.utils.APIResponse;
import net.zjitc.utils.JwtUtil;
import net.zjitc.utils.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")

public class AdminController {
    @Autowired
    private AdminService adminService;

//    public String  login(Admin admin, Model model, HttpSession session){
//        Admin user=adminService.findAdmin(admin.getUsername(),admin.getPassword());
//        if(user!=null){
////                页面显示信息
//            model.addAttribute("msg","登录成功");
//            session.setAttribute("admin",admin);
//            return "index";
//        }
//        model.addAttribute("msg","用户名错误");
//        return "login";
//    }
    @PostMapping("login")
    @ResponseBody
    public APIResponse<Token> login(@RequestBody Admin admin){
        Admin user=adminService.findAdmin(admin.getUsername(),admin.getPassword());
        APIResponse<Token> apiResponse = new APIResponse<>();
        if(user!=null){
            String token = JwtUtil.generateToken(user.getUsername());
            apiResponse.ok("登录成功",new Token(token));
            return apiResponse;
        }else{
            apiResponse.error(1000,"用户名错误",null);
            return apiResponse;
        }
    }
}
